<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>String | Oliver知识收集站</title>
    <meta name="generator" content="VuePress 1.9.7">
    
    <meta name="description" content="享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css" as="style"><link rel="preload" href="/oliver-vuepress/assets/js/app.c21e6ffc.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/3.6dd9a2a1.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/1.898920d0.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/23.12f0c72f.js" as="script"><link rel="prefetch" href="/oliver-vuepress/assets/js/10.41b2bf91.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/11.a95c117d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/12.8607f0e1.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/13.a52d6846.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/14.249b4e52.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/15.d458d12e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/16.ba334206.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/17.1b91c9fa.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/18.e2ea2eb5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/19.bf0e2553.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/20.268bd174.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/21.cd1bbed5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/22.da4bc7f7.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/24.b7886742.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/25.6e71af85.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/26.5c127243.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/27.e98fd8bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/28.ce83b09c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/29.50398f0f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/30.05e1339c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/31.ef4b13fb.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/32.ba5f8351.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/33.3902db0a.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/34.36a05884.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/35.87215872.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/36.db360c58.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/37.402e5374.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/38.c9228dd8.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/39.72ba5d1f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/4.7bb03d47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/40.7e7949bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/41.c0d5b947.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/42.d9984467.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/43.e6a43668.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/44.10d7fe47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/45.f692ec2d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/46.9b920343.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/47.8e3d94f9.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/48.7d356e5b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/49.b0df6271.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/5.1fa544da.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/50.805e1466.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/51.1b31d40e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/52.44e69a41.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/53.da1def53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/54.6569f7db.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/55.5fc3de47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/56.da649377.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/57.6ff15ed4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/58.a62f6424.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/59.f68ae517.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/6.f5bd8e9b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/60.dda416bc.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/61.4e0c719f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/62.8c5ef01e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/63.7089eb8b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/64.b5ec150d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/65.6720cda4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/66.4ee90e29.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/67.cc4b0c6d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/7.d5950c53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/8.382fb3a5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/9.d593f4c1.js">
    <link rel="stylesheet" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-130b300a><div data-v-130b300a><div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-130b300a data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Oliver知识收集站</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div class="hide" data-v-130b300a><header class="navbar" data-v-130b300a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/oliver-vuepress/" class="home-link router-link-active"><!----> <span class="site-name">Oliver知识收集站</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link router-link-active"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-130b300a></div> <aside class="sidebar" data-v-130b300a><div class="personal-info-wrapper" data-v-39576ba9 data-v-130b300a><!----> <h3 class="name" data-v-39576ba9>
    oliver.shi
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>52</h3> <h6 data-v-39576ba9>Articles</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>6</h3> <h6 data-v-39576ba9>Tags</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link router-link-active"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>基础篇</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/oliver-vuepress/articles/java/basics/oop.html" class="sidebar-link">面向对象</a></li><li><a href="/oliver-vuepress/articles/java/basics/enum.html" class="sidebar-link">枚举</a></li><li><a href="/oliver-vuepress/articles/java/basics/datatype.html" class="sidebar-link">数据类型</a></li><li><a href="/oliver-vuepress/articles/java/basics/string.html" aria-current="page" class="active sidebar-link">字符串</a></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>String</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div data-v-130b300a><main class="page"><section><div class="page-title"><h1 class="title">String</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>oliver.shi</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>1/1/2021</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>Java</span></i></div></div> <div class="theme-reco-content content__default"><h2 id="创建字符串的方式"><a href="#创建字符串的方式" class="header-anchor">#</a> 创建字符串的方式</h2> <div class="language-java extra-class"><pre class="language-java"><code># <span class="token number">1</span>、直接赋值
  <span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">&quot;abc&quot;</span>
 
# <span class="token number">2</span>、创建字符串对象  
  <span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">&quot;abc&quot;</span><span class="token punctuation">)</span>
  
</code></pre></div><h2 id="字符串的不可变性"><a href="#字符串的不可变性" class="header-anchor">#</a> 字符串的不可变性</h2> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">class</span> <span class="token class-name">String</span>
    <span class="token keyword">implements</span> <span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span>Serializable</span><span class="token punctuation">,</span> <span class="token class-name">Comparable</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">,</span> <span class="token class-name">CharSequence</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Stable</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> value<span class="token punctuation">;</span>
</code></pre></div><p>通过源码可知：</p> <p><code>String</code> 类 被 <code>final</code> 关键字修饰，表示不可继承 <code>String</code> 类</p> <p><code>String</code> 类的数据存储于 <code>byte[]</code> 数组，这个数组被 <code>final</code> 关键字修饰，表示 <strong><code>String</code> 对象不可被更改</strong></p> <blockquote><p>JDK 1.9 之前是使用 char 数组保存， 1.9 之后开始使用 byte 数组保存</p></blockquote> <h2 id="为什么-java-要这样设计"><a href="#为什么-java-要这样设计" class="header-anchor">#</a> 为什么 Java 要这样设计？</h2> <ul><li>线程安全
<ul><li>不可变的对象和值是线程安全的，可以实现多个线程中共享数据</li> <li>由于 String 天然的不可变，当一个线程”修改“了字符串的值，只会产生一个新的字符串对象，不会对其他线程的访问产生副作用，访问的都是同样的字符串数据，不需要任何同步操作</li></ul></li> <li>String 对象安全性，避免String被篡改
<ul><li>字符串用来创建存储敏感信息，如账号，密码，网络路径，文件处理等场景里，保证字符串 String 类的安全性就尤为重要了</li></ul></li> <li>可以实现字符串常量池
<ul><li>直接赋值
<ul><li>通过 <code>String str=&quot;abc&quot;</code> 方式创建时，JVM 首先会检测该对象是否在字符串常量池中存在，如果存在，则返回对象引用，否则会在常量池中创建新的字符串，这样做减少了同一个值的字符串对象重复被创建，节约内存</li></ul></li></ul></li> <li>Hashcode 缓存：
<ul><li>字符串的 <code>Hashcode</code> 属性不变，保证唯一性，使得类似 HashMap，HashSet 等容器才能实现相应的缓存功能，由于 String 的不可变，避免重复计算 <code>hashcode</code>，只有使用缓存的 <code>hashcode</code> 即可，这样一来大大提高了在散列集合中使用 String 对象的性能 (<strong>暂时不是很理解</strong>)</li></ul></li></ul> <h2 id="replacefirst-、replaceall、replace-区别"><a href="#replacefirst-、replaceall、replace-区别" class="header-anchor">#</a> replaceFirst 、replaceAll、replace 区别</h2> <ul><li>replaceFirst
<ul><li>和  <code>replaceAll</code> 使用 正则 进行字符串的替换，区别在 只替换第一个匹配到的项</li></ul></li> <li>replaceAll
<ul><li>使用 正则 ，进行替换所有的字符串</li></ul></li> <li>replace
<ul><li>指定字符串，替换所有相对应的字符串</li></ul></li></ul> <h2 id="字符串拼接的集中方式和区别"><a href="#字符串拼接的集中方式和区别" class="header-anchor">#</a> 字符串拼接的集中方式和区别</h2> <h3 id="_1、字符串运算符-拼接"><a href="#_1、字符串运算符-拼接" class="header-anchor">#</a> 1、字符串运算符 拼接</h3> <div class="language-java extra-class"><pre class="language-java"><code># <span class="token number">1.1</span> 两个字面量的拼接
<span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">&quot;a&quot;</span> <span class="token operator">+</span> <span class="token string">&quot;b&quot;</span><span class="token punctuation">;</span>
  
# <span class="token number">1.2</span> 一个字符串变量 和 字面量 拼接
<span class="token class-name">String</span> stra <span class="token operator">=</span> <span class="token string">&quot;stra&quot;</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> newStr <span class="token operator">=</span> stra <span class="token operator">+</span> <span class="token string">&quot;abc&quot;</span><span class="token punctuation">;</span>
</code></pre></div><ul><li>1.1:  两个字面量进行拼接时，编译器会进行常量折叠，进行自动拼接，即会变成 <code>String str = &quot;ab&quot;</code></li> <li>1.2:  字符串变量 和 字面量 进行拼接时， 是通过 StringBuilder 的 append() 方法进行字符串追加，最后在 调用 <code>toString()</code> 方法 转为字符串 （底层 就是一个new String()）</li></ul> <blockquote><p>对一个字符串 大量的拼接，会大大的降低性能</p></blockquote> <h3 id="_2、concat-拼接函数"><a href="#_2、concat-拼接函数" class="header-anchor">#</a> 2、<strong>concat</strong> 拼接函数</h3> <div class="language-java extra-class"><pre class="language-java"><code><span class="token class-name">String</span> stra <span class="token operator">=</span> <span class="token string">&quot;hello tom!&quot;</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> strb <span class="token operator">=</span> <span class="token string">&quot;hello oliver!&quot;</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> newStr <span class="token operator">=</span> stra<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>stra<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>strb<span class="token punctuation">)</span>
</code></pre></div><ul><li>创建一个字符串数组，长度是 已有字符串 和 待拼接字符串的长度，</li> <li>会将两个字符串的值，复制到新的字符数组中</li> <li>并使用这个数组 返回一个新的 <code>String</code>  对象</li></ul> <h3 id="_3、stringbuilder"><a href="#_3、stringbuilder" class="header-anchor">#</a> 3、StringBuilder</h3> <div class="language-java extra-class"><pre class="language-java"><code><span class="token class-name">StringBuilder</span> strBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuilder</span><span class="token punctuation">(</span><span class="token string">&quot;oliver&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
strBuilder<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">&quot;这是是一次测试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="_4、stringbuffer"><a href="#_4、stringbuffer" class="header-anchor">#</a> 4、StringBuffer</h3> <div class="language-java extra-class"><pre class="language-java"><code><span class="token class-name">StringBuffer</span> strBuffer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuffer</span><span class="token punctuation">(</span><span class="token string">&quot;oliver&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
strBuffer<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">&quot;这是是一次测试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="string-stringbuilder-string-buffer-的区别"><a href="#string-stringbuilder-string-buffer-的区别" class="header-anchor">#</a> String \ StringBuilder \ String Buffer 的区别</h2> <ul><li>String
<ul><li>不可变字符序列</li> <li>初始化可以赋 <code>null</code></li></ul></li> <li>StringBuiler
<ul><li>可变字符序列</li> <li>线程不安全</li> <li>效率高</li> <li>初始化可以赋 <code>null</code> 会报警，只允许是一个空的对象</li></ul></li> <li>StringBuffer
<ul><li>可变总府序列</li> <li>线程安全 （使用 <code>synchronized</code> 关键字修饰）</li> <li>效率低</li> <li>初始化可以赋 <code>null</code> 会报警，只允许是一个空的对象</li></ul></li></ul> <h2 id="string-对-switch-的支持"><a href="#string-对-switch-的支持" class="header-anchor">#</a> String 对 switch 的支持</h2> <h4 id="对字符型-char-类型的支持"><a href="#对字符型-char-类型的支持" class="header-anchor">#</a> 对字符型 char 类型的支持</h4> <p>char 类型的字符 反编译后，会变成 十进制的 <code>ascii码</code> , switch 实际还是比较的 Int 类型</p> <h4 id="对-字符串-string-类型的支持"><a href="#对-字符串-string-类型的支持" class="header-anchor">#</a> 对 字符串 String 类型的支持</h4> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> switchDemoString
<span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token function">switchDemoString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span> args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">&quot;world&quot;</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> s<span class="token punctuation">;</span>
        <span class="token keyword">switch</span><span class="token punctuation">(</span><span class="token punctuation">(</span>s <span class="token operator">=</span> str<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
        <span class="token keyword">default</span><span class="token operator">:</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token number">99162322</span><span class="token operator">:</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token number">113318802</span><span class="token operator">:</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">&quot;world&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;world&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>通过反编译可以了解到  switch 支持 String ，是通过 String 的 <code>hashcode</code>  和 <code>equals</code> 来进行对比的</p> <blockquote><p><strong>其实switch只支持一种数据类型，那就是整型，其他数据类型都是转换成整型之后再使用switch的</strong></p></blockquote> <h2 id="string-intern"><a href="#string-intern" class="header-anchor">#</a> String.intern</h2> <ul><li><code>&quot;a&quot;</code>  字面量会在字符串 存储在字符串常量池里，如果存在，则返回对象引用，否则会在常量池中创建新的字符串，这样做减少了同一个值的字符串对象重复被创建</li> <li><code>new String()</code>  会在堆中创建对象，并返回堆内存中的地址引用</li> <li><code>intern()</code> 方法会从字符串常量池中查询当前字符串是否存在，若不存在就会将当前字符串放入常量池中</li></ul> <h3 id="不同版本之间-intern-的区别"><a href="#不同版本之间-intern-的区别" class="header-anchor">#</a> 不同版本之间 intern 的区别</h3> <p>JDK6：</p> <p>常量池还是存在于永久代中。</p> <p>也就是说, <strong>常量池和堆是两块不同的内存区域。</strong>	因为两块内存区别不同，所以堆内的对象地址 和 常量池中对象地址是不相等的</p> <p>当使用 <code>intern</code> 方法时：</p> <p>​		如果存在： 则直接返回常量池的对象地址</p> <p>​		如果不存在：则会将堆内的字符串对象 复制一份 到常量池中，返回的是常量池中的对象地址</p> <p>JDK7 /JK8：</p> <p>从 JDK7 之后  字符串常量池已经被移到了堆当中了</p> <p>当使用 <code>intern</code> 方法时：</p> <p>​		如果存在： 则直接返回常量池的对象地址</p> <p>​		如果不存在：则会将堆内的字符串对象的引用地址 复制一份 到常量池中，返回的是常量池中的引用地址</p> <h2 id="string-有没有长度限制"><a href="#string-有没有长度限制" class="header-anchor">#</a> String 有没有长度限制</h2> <p>字符串是有有长度限</p> <ul><li><p>在编译期: 要求字符串常量池中的常量不能超过65535并且在javac执行过程中控制了最大值为65534。</p> <ul><li>即 通过<code>使用String s= “”;</code> 会有长度限制</li> <li>在定义常量时，编译成源码时，会将字符串 长度设置为 <code>u2 length</code></li> <li>u2表示两个字节的无符号数，那么1个字节有8位，2个字节就有16位。</li> <li>16位无符号数可表示的最大值位2^16 - 1 = 65535</li> <li>实际定义要小于 65535 即 最大值为 65534</li></ul></li> <li><p>在运行期，长度不能超过Int的范围，否则会抛异常</p> <ul><li>即 通过<code>使用new String();</code> 会有该限制</li> <li>通过 <code>new String()</code> 源码 得到 长度定义为 int  所有不得超过 int的范围</li></ul></li></ul> <h2 id="字符串常量池"><a href="#字符串常量池" class="header-anchor">#</a> 字符串常量池</h2> <blockquote><p>为什么会有字符串常量池:</p> <p>字符串的分配，和其他的对象分配一样，耗费高昂的时间与空间代价，作为最基础的数据类型，大量频繁的创建字符串，极大程度地影响程序的性能.</p> <p>JVM为了提高性能和减少内存开销</p></blockquote> <h3 id="字符串常量池的作用"><a href="#字符串常量池的作用" class="header-anchor">#</a> 字符串常量池的作用</h3> <ul><li>为字符串开辟一个字符串常量池，类似于缓存区</li> <li>创建字符串常量时，首先坚持字符串常量池是否存在该字符串</li> <li>存在该字符串，返回引用实例，不存在，实例化该字符串并放入池中</li></ul> <h3 id="字符串常量池存在什么地方"><a href="#字符串常量池存在什么地方" class="header-anchor">#</a> 字符串常量池存在什么地方：</h3> <ul><li>JDK6:  <code>永久代</code> 因此大小受到永久代的限制，默认1009大小，且不可更改，容易出现溢出问题</li> <li>JDK7:  转移到 <code>堆内存</code> 中 默认大小为60013， 此时可以通过<code>-XX:StringTableSize</code>参数进行控制大小</li> <li>JDk8:   转移到 <code>元空间</code> 中 ，直接使用的是本地内存</li></ul> <p><strong>相关好文：</strong></p> <p><a href="https://blog.csdn.net/tyyking/article/details/82496901" target="_blank" rel="noopener noreferrer">几张图轻松理解String.intern()<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p><a href="https://tech.meituan.com/2014/03/06/in-depth-understanding-string-intern.html" target="_blank" rel="noopener noreferrer">深入解析String#intern<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新: </span> <span class="time">4/21/2022, 8:17:59 AM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/oliver-vuepress/articles/java/basics/datatype.html" class="prev">
            数据类型
          </a></span> <!----></p></div> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#创建字符串的方式" class="sidebar-link reco-side-创建字符串的方式" data-v-cb1513f6>创建字符串的方式</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#字符串的不可变性" class="sidebar-link reco-side-字符串的不可变性" data-v-cb1513f6>字符串的不可变性</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#为什么-java-要这样设计" class="sidebar-link reco-side-为什么-java-要这样设计" data-v-cb1513f6>为什么 Java 要这样设计？</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#replacefirst-、replaceall、replace-区别" class="sidebar-link reco-side-replacefirst-、replaceall、replace-区别" data-v-cb1513f6>replaceFirst 、replaceAll、replace 区别</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#字符串拼接的集中方式和区别" class="sidebar-link reco-side-字符串拼接的集中方式和区别" data-v-cb1513f6>字符串拼接的集中方式和区别</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#_1、字符串运算符-拼接" class="sidebar-link reco-side-_1、字符串运算符-拼接" data-v-cb1513f6>1、字符串运算符 拼接</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#_2、concat-拼接函数" class="sidebar-link reco-side-_2、concat-拼接函数" data-v-cb1513f6>2、concat 拼接函数</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#_3、stringbuilder" class="sidebar-link reco-side-_3、stringbuilder" data-v-cb1513f6>3、StringBuilder</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#_4、stringbuffer" class="sidebar-link reco-side-_4、stringbuffer" data-v-cb1513f6>4、StringBuffer</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#string-stringbuilder-string-buffer-的区别" class="sidebar-link reco-side-string-stringbuilder-string-buffer-的区别" data-v-cb1513f6>String \ StringBuilder \ String Buffer 的区别</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#string-对-switch-的支持" class="sidebar-link reco-side-string-对-switch-的支持" data-v-cb1513f6>String 对 switch 的支持</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#string-intern" class="sidebar-link reco-side-string-intern" data-v-cb1513f6>String.intern</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#不同版本之间-intern-的区别" class="sidebar-link reco-side-不同版本之间-intern-的区别" data-v-cb1513f6>不同版本之间 intern 的区别</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#string-有没有长度限制" class="sidebar-link reco-side-string-有没有长度限制" data-v-cb1513f6>String 有没有长度限制</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#字符串常量池" class="sidebar-link reco-side-字符串常量池" data-v-cb1513f6>字符串常量池</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#字符串常量池的作用" class="sidebar-link reco-side-字符串常量池的作用" data-v-cb1513f6>字符串常量池的作用</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/string.html#字符串常量池存在什么地方" class="sidebar-link reco-side-字符串常量池存在什么地方" data-v-cb1513f6>字符串常量池存在什么地方：</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/oliver-vuepress/assets/js/app.c21e6ffc.js" defer></script><script src="/oliver-vuepress/assets/js/3.6dd9a2a1.js" defer></script><script src="/oliver-vuepress/assets/js/1.898920d0.js" defer></script><script src="/oliver-vuepress/assets/js/23.12f0c72f.js" defer></script>
  </body>
</html>
